package elka.gis.postman;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;


import elka.gis.postman.bean.Edge;
import elka.gis.postman.bean.Graph;
import elka.gis.postman.bean.Path;
import elka.gis.postman.common.Constants;
import elka.gis.postman.helper.GraphHelper;
import elka.gis.postman.helper.GraphReader;

/**
 * Klasa uruchamiajaca aplikacje
 * @author Wojtek
 *
 */
public class PostmanApp {
	private static final Logger LOGGER = Logger.getLogger(PostmanApp.class);
	
	static {
		try {
			PropertyConfigurator.configure (new ClassPathResource(Constants.LOG4J_CONFIG_PATH).getInputStream());
		} catch (IOException e) {
			LOGGER.error("Blad czytania pliku konfiguracji logowania");
		}
	}
	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
				"application-config.xml"});
		
		GraphReader graphReader = context.getBean("graphReader", GraphReader.class);
		GraphHelper graphHelper = context.getBean("graphHelper", GraphHelper.class);
		
		String graphPath = args[0];
		Graph graph = graphReader.readGraphFromPath(graphPath);
		LOGGER.info("Wczytano graf");
		
		Path path = graphHelper.findPostmanPath(graph);
		LOGGER.info("Znaleziono sciezke");
		
		LOGGER.info("Sciezka:");
		for(Edge edge:path.getEdges()) {
			LOGGER.info(edge);
		}
		
		
		LOGGER.info("Zapis sciezki do pliku path.json");
		graphReader.savePath(path);
	}
}
